package com.web.controller;


import com.jdbc.DataSourceTool;
import com.web.bean.教师情况;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/TcrExcelServlet")
public class TcrExcelServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {

        response.setHeader("Content-Encoding", "gb2312");
        response.setContentType("application/vnd.ms-excel;charset=gb2312");
        PrintWriter out = response.getWriter();

        Connection connection = DataSourceTool.getConnection();
       
        String sql = "select * from 教师情况;";
        out.println("教师编号\t姓名\t性别\t职称\t院系代号");

        try {
            PreparedStatement ps = connection.prepareStatement(sql);

            ResultSet rs = ps.executeQuery();

            while(rs.next()){

                教师情况 stu=new 教师情况();
                stu.set教师编号(rs.getString(1));
                stu.set姓名(rs.getString(2));
                stu.set性别(rs.getString(3));
                stu.set职称(rs.getString(4));
                stu.set院系代号(rs.getString(5));


                //把查询结果输出到excel中
                out.print(stu.get教师编号()+"\t"+stu.get姓名()+"\t"+stu.get性别()+"\t"+stu.get职称()+"\t"+stu.get院系代号());
                out.println();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }

    }
}
